<template>
  <div class="border">
    <div :class="['box', { clearbg: imgFlag }]">
      <div v-if="imgFlag" class="box_h">
        <div class="user">
          <img
            v-if="imgFlag"
            :src="$axios.defaults.baseURL + data.account.defaultAvatar"
            alt
          />
          {{ data.account.nickname }}
          <span class="time">{{ time }}</span>
        </div>
        <div class="deep">{{ data.level }}</div>
      </div>

      <div :class="['parent', { active: imgFlag }]">
        <CommentItem
          @sendReply="ReplySend"
          :imgFlag="false"
          v-if="data.parent"
          :data="data.parent"
        />
        <div v-if="!imgFlag" class="box_h">
          <div class="user">
            <img
              v-if="imgFlag"
              :src="$axios.defaults.baseURL + data.account.defaultAvatar"
              alt
            />
            {{ data.account.nickname }}
            <span>{{ time }}</span>
          </div>
          <div class="deep">{{ data.level }}</div>
        </div>
        <div class="box_b" @mouseleave="flag = false" @mouseover="flag = true">
          <div class="content">{{ data.content }}</div>
          <div class="imgList">
            <div class="item" v-for="(item, index) in data.pics" :key="index">
              <img :src="$axios.defaults.baseURL + item.url" alt />
            </div>
          </div>
          <div v-if="flag" @click="reply()" class="reply">回复</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
export default {
  props: {
    data: Object,
    imgFlag: {
      type: Boolean,
      default: true,
    },
  },
  name: 'CommentItem',
  data() {
    return {
      flag: false,
    }
  },
  mounted() {
    // console.log(this.data)
  },
  created() {
    this.getdeep(this.data)
  },
  methods: {
    reply() {
      const obj = {
        id: this.data.id,
        nickname: this.data.account.nickname,
      }
      this.$emit('sendReply', obj)
    },
    ReplySend(obj) {
      this.$emit('sendReply', obj)
    },
    getdeep(data) {
      if (data.parent) {
        this.deep += 1
        this.getdeep(data.parent)
      }
    },
  },
  computed: {
    time() {
      return moment(this.data.created_at).format('YYYY-MM-DD HH:mm')
    },
  },
}
</script>
<style lang="less" scoped>
.clearbg {
  background: #fff !important;
  border: none !important;
  padding-bottom: 10px !important;
}
.border {
  border-bottom: 1px dashed #ddd;
}
.active {
  padding: 0 20px 0 30px;
}
.deep {
  color: #666;
  font-size: 10px;
}
.box {
  border: 1px solid #ddd;
  padding: 1px;
  background: #f9f9f9;

  .box_h {
    margin: 0 10px;
    height: 30px;
    line-height: 30px;
    display: flex;
    justify-content: space-between;
    .user {
      display: flex;
      align-items: center;
      font-size: 14px;
      .time {
        font-size: 10px;
        color: #aaa;
      }
      img {
        margin-right: 5px;
        width: 20px;
        height: 20px;
        border-radius: 50%;
      }
      span {
        margin-left: 5px;
        font-size: 12px;
      }
    }
  }
  .box_b {
    position: relative;
    .reply {
      cursor: pointer;
      position: absolute;
      right: 10px;
      bottom: 10px;
      font-size: 12px;
      color: blue;
    }
    .content {
      padding: 10px 20px;
    }
    .imgList {
      padding: 0 20px;
      display: flex;
      .item {
        margin-right: 10px;
        width: 90px;
        height: 90px;
        border: 1px dashed #ddd;
        border-radius: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;
        img {
          width: 75px;
          height: 75px;
          object-fit: cover;
        }
      }
    }
  }
}
</style>